package service.faculty;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import util.manager.PageManager;
import util.paramvo.SearchParamVO;
import util.returnvo.FacultySearchListReturnVO;
import vo.FacultyVO;
import config.PageConfig;
import dao.faculty.FacultyDAO;

public class FacultyServiceImpl implements FacultyService{

	private FacultyDAO facultyDAO;

	public void setFacultyDAO(FacultyDAO facultyDAO) {
		this.facultyDAO = facultyDAO;
	}
	
	@Override
	public FacultySearchListReturnVO getSearchedFacultyByWord(String search, String page) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<FacultyVO> facultylist=facultyDAO.getSearchedFacultyByWord(new SearchParamVO(searchlist, PageConfig.POPUP_ROW_PER_PAGE, pageNo));
		
		return new FacultySearchListReturnVO(facultylist, new PageManager(PageConfig.POPUP_ROW_PER_PAGE, PageConfig.POPUP_PAGE_PER_GROUP, facultyDAO.countSearchedFacultyByWord(searchlist), pageNo));
	}

	@Override
	public List<FacultyVO> getFacultyEditList(String search) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<FacultyVO>();
		
		return facultyDAO.getSearchedFacultyByWord(new SearchParamVO(searchlist, PageConfig.EDIT_ROW_PER_PAGE, 1));
	}

	@Override
	public List<String> autoCompleteFacultyList(String search) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<String>();
		
		List<FacultyVO> facultylist=facultyDAO.getSearchedFacultyByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
		
		ArrayList<String> returnlist=new ArrayList<String>();
		
		for(FacultyVO vo : facultylist){
			returnlist.add(vo.getFaculty_name());
		}
		
		return returnlist;
	}

	@Override
	public void registerFaculty(String Faculty_name) throws Exception {
		facultyDAO.registerFaculty(Faculty_name);
	}

	@Override
	public void updateFaculty(FacultyVO param) throws Exception {
		facultyDAO.updateFaculty(param);
	}

	@Override
	public void deleteFaculty(int faculty_no) throws Exception {
		facultyDAO.deleteFaculty(faculty_no);
	}
}
